package com.whuan.hystrix.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class HystrixController {

    //@HystrixCommand(fallbackMethod = "getDefault")
    @GetMapping("/msg1")
    public String getMsg(){
        throw new IbestException(1,"程序异常了呀");
        //throw new RuntimeException("程序异常了呀");
        //return "this is msg1";
    }
    @GetMapping("/msg2")
    public String getMsg2(){
        return "this is msg2";
    }
    @GetMapping("/msg3")
    public String getMsg3(@RequestParam String msg){
        return "this is msg3 ：" + msg ;
    }
    public String getDefault(){
        return "this is getDefault";
    }

    @HystrixCommand(fallbackMethod="helloBackMethodFirst")
    public String  helloService() {
        log.info("start invoke service");
        throw new RuntimeException("consumer exception");
    }

    public String helloBackMethodFirst(Throwable e) {
        // 一些异常判断
        if (e instanceof RuntimeException) {
            log.info("error");
        }
        if (e instanceof IllegalStateException) {
        }

        return "error2:" + e.getMessage();
    }
}
